﻿
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using Xc.DataAccess.Core;
using Xc.DataAccess.DataBase.DeviceInfos;
using Xc.DataAccess.DataBase.DeviceInfos.Models;

namespace Xc.WaterMeters.ServerHosts.Controllers
{
    /// <summary>
    /// 设备报警信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceInfoAlarmController:ControllerBase
    {
        private readonly IDbDeviceInfoAlarmService dbDeviceInfoAlarmService;
        /// <summary>
        /// 设备报警信息
        /// </summary>
        /// <param name="dbDeviceInfoAlarmService"></param>
        public DeviceInfoAlarmController(IDbDeviceInfoAlarmService dbDeviceInfoAlarmService)
        {
            this.dbDeviceInfoAlarmService = dbDeviceInfoAlarmService;
        }
        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetAlarms([FromBody] Dictionary<string, UnitString> valuePairs, string deviceId)
        {
            return await dbDeviceInfoAlarmService.Set(valuePairs, deviceId);
        }
        /// <summary>
        /// 获取设备属性
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public ResultData<AlarmInfo[]> GetAlarms(string deviceId)
        {
            return dbDeviceInfoAlarmService.Gets(deviceId);
        }
        /// <summary>
        /// 删除指定报警信息
        /// </summary>
        /// <param name="deviceId">设备编号</param>
        /// <param name="name">属性名称</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<Result> Delete(string deviceId, string name)
        {
            return await dbDeviceInfoAlarmService.Remove(deviceId, name);
        }
        /// <summary>
        /// 获取历史报警信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="dateTimeStart"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AlarmInfoHistory[]> GetAlarmsHistory(string deviceId, DateTime? dateTimeStart, DateTime? dateTimeEnd)
        {
            return await dbDeviceInfoAlarmService.Gets(deviceId, dateTimeStart, dateTimeEnd);
        }
        /// <summary>
        /// 获取属性表
        /// </summary>
        /// <param name="deviceId">设备编号</param>
        /// <param name="autoAppendLastState">自动补齐最新状态</param>
        /// <param name="dateTimeStart">开始时间</param>
        /// <param name="dateTimeEnd">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<DataTableInfo> GetAlarmsTable(string deviceId, DateTime? dateTimeStart, DateTime? dateTimeEnd, bool autoAppendLastState = true)
        {
            return await dbDeviceInfoAlarmService.GetTable(deviceId, autoAppendLastState, dateTimeStart, dateTimeEnd);
        }
    }
}
